package javacommon.util.org;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javacommon.util.CtgData;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.model.Dept;
import com.tech15.core.model.Module;
import com.tech15.core.model.SysUser;
import com.tech15.pub.model.PubData;

public class TreeUtils {
	
	public static List<Dept> setDeptTreeList(Dept dept){
		List<Dept> newList = new ArrayList<Dept>();
		Dept d = new Dept();
		d.setDeptname("├"+dept.getDeptname());
		d.setDeptid(dept.getDeptid());
		newList.add(d);
		
		Set children = dept.getDepts();   
		for (Iterator iter = children.iterator(); iter.hasNext();){   
        	Dept child = (Dept) iter.next();  
        	Dept c_d = new Dept();
        	c_d.setDeptname("│  ├"+child.getDeptname());
        	c_d.setDeptid(child.getDeptid());
    		newList.add(c_d);
    		
        	/*List<Dept> items = setDeptTreeList(child);
        	newList.addAll(items);*/
		}
		
		return newList;
	}
	/**
	 * 获取部门树状数据
	 * @param dept
	 * @return
	 */
	public static TreeData setDeptContent(Dept dept){
		List<UserData> userdata = new ArrayList<UserData>();
		
		UserData ud = new UserData();
		ud.setName("id");
		ud.setContent(dept.getDeptid());
		userdata.add(ud);
		
		ud = new UserData();
		ud.setName("sortNO");
		ud.setContent(dept.getSortno());
		userdata.add(ud);
		
		ud = new UserData();
		ud.setName("memo");
		if(ObjectUtils.isNullOrEmptyString(dept.getMemo())){
			ud.setContent("");
		}else{
			ud.setContent(dept.getMemo());
		}
		userdata.add(ud);
		
		ud = new UserData();
		ud.setName("pid");
		ud.setContent(dept.getPid());
		userdata.add(ud);
		
		TreeData td = new TreeData();
		td.setUserdata(userdata);
		td.setId("DPT"+dept.getDeptid());
		td.setText(dept.getDeptname());
		
		
		List<TreeData> item = new ArrayList<TreeData>();
		Set children = dept.getDepts();   
		for (Iterator iter = children.iterator(); iter.hasNext();){   
        	Dept child = (Dept) iter.next();  
        	TreeData child_td = setDeptContent(child);
        	item.add(child_td);
		}
		td.setItem(item);
		
		return td;
	}

	/**
	 * 获取模块树状数据
	 * @param mod
	 * @return
	 */
	public static CtgData setModuleContent(Module mod){
		CtgData ctg = new CtgData();
		ctg.setId(""+mod.getMdid());
		ctg.setText(mod.getName());
		ctg.setContent(mod.getRemark());
		ctg.setEn(false);
		
		Set children = mod.getModules();
		
		List<CtgData> item = new ArrayList<CtgData>();
		for (Iterator iter = children.iterator(); iter.hasNext();){
			Module child = (Module)iter.next();
			CtgData child_ctg = setModuleContent(child);
			item.add(child_ctg);
		}
		ctg.setItem(item);
		return ctg;
	}

	/**
	 * 获取模块树状数据-人员最终权限列表
	 * @param mod
	 * @return
	 */
	public static List<CtgData> setFinalModuleContent(List<Module> mdlist){
		List<CtgData> list = new ArrayList<CtgData>();
		for(Module mod : mdlist){
			if(mod.getLevel() == 1){
				CtgData ctg = new CtgData();
				ctg.setId(""+mod.getMdid());
				ctg.setText(mod.getName());
				ctg.setEn(false);
				list.add(ctg);
			}
		}
		for(CtgData ctg : list){
			List<CtgData> item = new ArrayList<CtgData>();
			for(Module mod : mdlist){
				if(mod.getLevel() == 2){
					if(new Long(ctg.getId()).longValue() == mod.getPid().longValue()){
						CtgData child = new CtgData();
						child.setId(""+mod.getMdid());
						child.setText(mod.getName());
						child.setMy(mod.getMy());
						child.setDept(mod.getDept());
						child.setPg(mod.getPg());
						child.setEn(false);
						if(mod.getIsAuth() == 1){
							child.setIfCheck(true);
						}else{
							child.setIfCheck(false);
						}
						item.add(child);
					}
				}
			}
			ctg.setItem(item);
		}
		return list;
	}

	/**
	 * 获取模块树状数据
	 * @param mod
	 * @return
	 */
	public static List<CtgData> setModuleContent(List<Module> mdlist){
		List<CtgData> list = new ArrayList<CtgData>();
		for(Module mod : mdlist){
			if(mod.getLevel() == 1){
				CtgData ctg = new CtgData();
				ctg.setId(""+mod.getMdid());
				ctg.setText(mod.getName());
				ctg.setContent(mod.getRemark());
				ctg.setEn(false);
				list.add(ctg);
			}
		}
		for(CtgData ctg : list){
			List<CtgData> item = new ArrayList<CtgData>();
			for(Module mod : mdlist){
				if(mod.getLevel() == 2){
					if(new Long(ctg.getId()).longValue() == mod.getPid().longValue()){
						CtgData child = new CtgData();
						child.setId(""+mod.getMdid());
						child.setText(mod.getName());
						child.setContent(mod.getRemark());
						child.setEn(false);
						if(mod.getIsAuth() == 1){
							child.setIfCheck(true);
						}else{
							child.setIfCheck(false);
						}
						item.add(child);
					}
				}
			}
			ctg.setItem(item);
		}
		return list;
	}

	/**
	 * 获取部门与用户结合的树，设置部门的用户列表信息，已TreeData对象保存
	 * @param dept
	 * @return
	 */
	public static List<TreeData> setUserDatas(List<SysUser> users) {
		List<TreeData> item = new ArrayList<TreeData>();
		for(SysUser user : users){
			TreeData td = new TreeData();
			td.setId("user_"+user.getUsername());
			td.setText(user.getRealname());
			item.add(td);
		}
		return item;
	}

	/*public static CtgData setSelectWflowCtgContent(WorkflowCategory wfc) {
		CtgData ctg = new CtgData();
		ctg.setId(""+wfc.getWfcid());
		ctg.setText(wfc.getCtgname());
		
		Set children = wfc.getWorkflowCategorys();
		
		List<CtgData> item = new ArrayList<CtgData>();
		for (Iterator iter = children.iterator(); iter.hasNext();){
			WorkflowCategory child = (WorkflowCategory)iter.next();
			CtgData child_ctg = setSelectWflowCtgContent(child);
			item.add(child_ctg);
		}
		ctg.setItem(item);
		return ctg;
	}*/

	public static TreeData setDataContent(PubData pd) {
		List<UserData> userdata = new ArrayList<UserData>();
		
		UserData ud = new UserData();
		ud.setName("id");
		ud.setContent(pd.getId());
		userdata.add(ud);
		
		ud = new UserData();
		ud.setName("sortno");
		ud.setContent(pd.getSortno());
		userdata.add(ud);
		
		ud = new UserData();
		ud.setName("pid");
		ud.setContent(pd.getPid());
		userdata.add(ud);
		
		TreeData td = new TreeData();
		td.setUserdata(userdata);
		td.setId("PD"+pd.getId());
		td.setText(pd.getName());
		
		
		List<TreeData> item = new ArrayList<TreeData>();
		Set children = pd.getPubDatas();
		for (Iterator iter = children.iterator(); iter.hasNext();){   
			PubData child = (PubData) iter.next();  
        	TreeData child_td = setDataContent(child);
        	item.add(child_td);
		}
		td.setItem(item);
		
		return td;
	}


	
}
